package com.wondertek.poms.core.filter.impl;

import java.util.List;

import com.wondertek.poms.core.filter.BaseRuleFilter;
import com.wondertek.poms.core.filter.RuleFilterChain;
import org.apache.commons.lang3.StringUtils;

import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.IRulePo;
import com.wondertek.poms.dao.po.PrdInfoRule;


/**
 * <p><b>MediaUsageCodeFilter Description:</b> 码率过滤器</p>
 * @author douzi
 * <b>DATE</b> 2020年11月16日 下午3:45:45
 */
public class MediaUsageCodeFilter extends BaseRuleFilter {

	@Override
	public void doFilter(IRulePo rule, Content c, RuleFilterChain chain) {
		if (rule instanceof PrdInfoRule) {
			PrdInfoRule r = (PrdInfoRule) rule;
			this.process(r.getPrdRuleId(),
					r.getMediaUsageCode(),
					chain,
					"码率-MediaUsageCode", c);
		}

		chain.doFilter(rule, c, chain);
	}
	
	void process(Long id, String ruleStr, RuleFilterChain chain, String comments, Content c) {
		if (chain.isFlag() && StringUtils.isNotBlank(ruleStr)) {
			List<String> mediaUsageCodeList = c.getMediaUsageCodeList();
			if (mediaUsageCodeList == null || mediaUsageCodeList.size() <= 0) {
				ruleStr = ruleStr.trim();
				chain.setFlag(Boolean.FALSE);
	            log(id, chain, comments, ruleStr, null);
			} else {
	            ruleStr = ruleStr.trim();
	            StringBuffer usageCode = new StringBuffer();
				for (int i = 0; i < mediaUsageCodeList.size(); i++) {
					if (i == 0) {
						usageCode.append(mediaUsageCodeList.get(i));
					} else {
						usageCode.append("," + mediaUsageCodeList.get(i));
					}
				}
	            
	            chain.setFlag(Boolean.FALSE);
                String ruleTemp = ruleStr.replaceAll("，", ",");
                String voTemp = usageCode.toString().replaceAll("，", ",");
                voTemp = "," + voTemp + ",";  // 把 '电影,电视剧' 修改为 ',电影,电视剧,'
                String[] ruleRmds = ruleTemp.split(",");
                for (String rmd : ruleRmds) {
                    rmd = "," + rmd + ","; // 把 '电' 修改为 ',电,' 这样就不会导致contains 一个电就符合规则了
                    if (voTemp.contains(rmd)) {
                        chain.setFlag(Boolean.TRUE);
                        break;
                    }
                }
                
	            log(id, chain, comments, ruleStr, usageCode.toString());
			}
        }
    }
}
